package com.zxg.util.fanyi;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.taobao.api.internal.util.WebUtils;

public class FanyiUtil {
  final static Logger log = LoggerFactory.getLogger("manager");

  public static void main(String[] args) {
    String s0 = "你好，我很喜欢你";
    System.out.println(s0.length());
    String s = "高-田老虎头-家的针织衫-同样是今年柜子上的最新款-砖贵售价-咱们的价格真心是逆天级别的-此款面料是全棉的-彩色提花工艺也非常好看-做工-细节-都是很细致的-抛开品-因素不说-块钱买一件这种质量的针织衫也是超值的-关键这个提花设计-还非常的";
    System.out.println(s.length());
    System.out.println(FanyiUtil.translate2English(s0));
  }

  public static String translate2English(String chinese) {
    String url = "http://fanyi.youdao.com/openapi.do?keyfrom=manongsoft001&key=1709205305&type=data&doctype=json&version=1.1";
    Map<String, String> params = new HashMap<String, String>();
    params.put("q", chinese);
    try {
      String response = WebUtils.doPost(url, params, 2000, 2000);
      Webexplain explain = JSON.parseObject(response, Webexplain.class);
      if (explain.getTranslation() != null && explain.getTranslation().size() > 0) {
        String tran = explain.getTranslation().get(0);
        log.warn("yd chinese:" + chinese + " en:" + tran);
        return tran;
      }
    } catch (Exception e) {
      return null;
    }
    // log.error("translation error return baidunew");
    return null;

  }

  static Random r = new Random();

  public static Webexplain translate2EnglishYD(String chinese, String key, String fromkey) {
    String url = "http://fanyi.youdao.com/openapi.do?keyfrom=" + fromkey + "&key=" + key + "&type=data&doctype=json&version=1.1";
    Map<String, String> params = new HashMap<String, String>();
    params.put("q", chinese);
    try {
      String response = WebUtils.doGet(url, params);
      Webexplain explain = JSON.parseObject(response, Webexplain.class);
      if (explain.getTranslation() != null && explain.getTranslation().size() > 0) {
        String tran = explain.getTranslation().get(0);
        log.warn("yd2 chinese:" + chinese + " en:" + tran);
        return explain;
      }
    } catch (Exception e) {
      log.error("translation error ", e);
      return null;
    }
    return null;
  }

  public static String buildRequestMysign(String appid, String q, String salt, String key) {
    String prestr = appid + q + salt + key; // 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
    String mysign = "";

    mysign = MD5(prestr);

    return mysign;
  }

  public final static String MD5(String s) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    try {
      byte[] btInput = s.getBytes();
      // 获得MD5摘要算法的 MessageDigest 对象
      MessageDigest mdInst = MessageDigest.getInstance("MD5");
      // 使用指定的字节更新摘要
      mdInst.update(btInput);
      // 获得密文
      byte[] md = mdInst.digest();
      // 把密文转换成十六进制的字符串形式
      int j = md.length;
      char str[] = new char[j * 2];
      int k = 0;
      for (int i = 0; i < j; i++) {
        byte byte0 = md[i];
        str[k++] = hexDigits[byte0 >>> 4 & 0xf];
        str[k++] = hexDigits[byte0 & 0xf];
      }
      return new String(str);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
}
